﻿//
// Copyright (C) 2010 Tarmin Rehve <tarmin.rehve@gmail.com>
//
//This file is part of the XDataGridView software library.
//
//    XDataGridView is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    XDataGridView is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Lesser License for more details.
//
//    You should have received a copy of the GNU Lesser Public License
//    along with XDataGridView.  If not, see <http://www.gnu.org/licenses/>.


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace XControls
{
    /// <summary>
    /// Base class of <see cref="XDataGridViewRowCursor"/> and <see cref="XDataGridViewColumnCursor"/>
    /// Internal purpose only, this class should not be used directly.
    /// </summary>
    /// <typeparam name="DATAGRID_VIEW_BAND_COLLECTION">Can be of type <see cref="DataGridViewRowCollection"/> or <see cref="DataGridViewColumnCollection"/></typeparam>
    /// <typeparam name="XDATAGRID_VIEW_CURSOR">Can be of type <see cref="XDataGridViewRowCursor"/> or <see cref="XDataGridViewColumnCursor"/></typeparam>
    [Browsable(false)]
    public class XDataGridViewCursorCollection<DATAGRID_VIEW_BAND_COLLECTION, XDATAGRID_VIEW_CURSOR> :
        List<XDATAGRID_VIEW_CURSOR>
        where XDATAGRID_VIEW_CURSOR : XDataGridViewCursor<DATAGRID_VIEW_BAND_COLLECTION>, new()
        where DATAGRID_VIEW_BAND_COLLECTION : System.Collections.IList, System.Collections.IEnumerable, System.Collections.ICollection
    {
        readonly protected XDataGridView OwningXDataGridView;

        internal XDataGridViewCursorCollection(XDataGridView xDataGridView)
        {
            OwningXDataGridView = xDataGridView;
        }
    };

    /// <summary>
    /// A collection of <see cref="XDataGridViewRowCursor"/>.
    /// </summary>
    public class XDataGridViewRowCursorCollection : XDataGridViewCursorCollection<DataGridViewRowCollection, XDataGridViewRowCursor>
    {
        internal XDataGridViewRowCursorCollection(XDataGridView xDataGridView) : base(xDataGridView)
        {
            xDataGridView.RowsAdded += new DataGridViewRowsAddedEventHandler(xDataGridView_RowsAdded);
            xDataGridView.RowsRemoved += new DataGridViewRowsRemovedEventHandler(xDataGridView_RowsRemoved);
        }

        void xDataGridView_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            int firstRemovedRowIndex = e.RowIndex;
            int countOfRowsRemoved = e.RowCount;

            foreach (XDataGridViewRowCursor rowCursor in this)
                if (rowCursor.Position > firstRemovedRowIndex)
                    rowCursor.UpdatePosition(rowCursor.Position - countOfRowsRemoved);
        }

        void xDataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            int firstAddedRowIndex = e.RowIndex;
            int countOfRowsAdded = e.RowCount;

            foreach (XDataGridViewRowCursor rowCursor in this)                
                if (rowCursor.Position > firstAddedRowIndex)
                    rowCursor.UpdatePosition(rowCursor.Position + countOfRowsAdded);
        }

        /// <summary>
        /// Add a new row cursor to the collection
        /// </summary>
        /// <returns>The index of the new row cursor</returns>
        public int Add()
        {
            XDataGridViewRowCursor rowCursor = new XDataGridViewRowCursor();
            rowCursor.UnderlyingBandCollection = OwningXDataGridView.Rows;
            Add(rowCursor);
            return Count - 1;
        }
        /// <summary>
        /// Add the specified row cursor in the collection
        /// </summary>
        /// <param name="cursor">The row cursor to add to the collection</param>
        /// <returns>The index of the row cursor</returns>
        public new int Add(XDataGridViewRowCursor cursor)
        {
            cursor.UnderlyingBandCollection = OwningXDataGridView.Rows;
            base.Add(cursor);
            return Count - 1;
        }
    };
    /// <summary>
    /// A collection of <see cref="XDataGridViewColumnCursor"/>.
    /// </summary>
    public class XDataGridViewColumnCursorCollection :
        XDataGridViewCursorCollection<DataGridViewColumnCollection, XDataGridViewColumnCursor>
    {
        internal XDataGridViewColumnCursorCollection(XDataGridView xDataGridView) : base(xDataGridView) { }

        /// <summary>
        /// Add a new column cursor to the collection
        /// </summary>
        /// <returns>The index of the new column cursor</returns>
        public int Add()
        {
            XDataGridViewColumnCursor columnCursor = new XDataGridViewColumnCursor();
            columnCursor.UnderlyingBandCollection = OwningXDataGridView.Columns;
            Add(columnCursor);
            return Count - 1;
        }

        /// <summary>
        /// Add the specified column cursor in the collection
        /// </summary>
        /// <param name="cursor">The column cursor to add to the collection</param>
        /// <returns>The index of the column cursor</returns>
        public new int Add(XDataGridViewColumnCursor cursor)
        {
            cursor.UnderlyingBandCollection = OwningXDataGridView.Columns;
            base.Add(cursor);
            return Count - 1;
        }
    };
}
